package com.example.mqdemo.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.IdUtil;
import com.example.mqdemo.bean.MsgData;
import com.example.mqdemo.common.ExchangeConstant;
import com.example.mqdemo.common.RoutingConstant;
import com.example.mqdemo.util.MsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @Autowired
    MsgUtil msgUtil;

    @GetMapping(value = "msg")
    public void sendOrderMsgWithDirect(@RequestParam Object param) {
        String uid = IdUtil.fastUUID();

        MsgData msgData = new MsgData();
        msgData.setStatus("begin");
        msgData.setId(uid);

        msgUtil.setData(uid, msgData);

        CorrelationData correlationData = new CorrelationData(uid);
        rabbitTemplate.convertAndSend(ExchangeConstant.orderChangeName, RoutingConstant.orderRoutingKey, param, correlationData);
    }

    @GetMapping(value = "cancel")
    public void cancelOrderMsgWithDirect(@RequestParam Object param) {
        String uid = IdUtil.fastUUID();

        MsgData msgData = new MsgData();
        msgData.setStatus("begin");
        msgData.setId(uid);

        msgUtil.setData(uid, msgData);

        CorrelationData correlationData = new CorrelationData(uid);
        rabbitTemplate.convertAndSend(ExchangeConstant.orderCancelDeadChangeName, RoutingConstant.orderCancelDeadRoutingKey, param, correlationData);
    }

    @GetMapping(value = "data")
    public Map<String, MsgData> msgData() {
        return msgUtil.getData();
    }

    @GetMapping(value = "rocket")
    public void rocketMsg(@RequestParam String param) {

        // 创建订单号
        String orderId = "1234567" + param;

        // 构建自定义消息头
        MessageHeaders headers = new MessageHeaders(MapUtil.<String, Object>builder().put("orderId", orderId).build());

        // 构建消息
        Message<String> msg = MessageBuilder.createMessage(param, headers);

        // 普通发送
//        rocketMQTemplate.asyncSend("orderCreate", msg, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                log.info("消息:{}发送成功:{}", sendResult.getMsgId(), sendResult.getMsgId());
//            }
//
//            @Override
//            public void onException(Throwable e) {
//                log.info("消息发送失败:{}", e.getLocalizedMessage());
//            }
//        });

        // 事务发送
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("orderCreate", msg, "123456");
        System.out.println("[result = " + result.getMsgId() + " / " + result.getSendStatus().toString() + "]");
    }
}
